package com.thirdsdk.umeng;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;

import com.curry.android.http.URLHelper;
import com.curry.android.util.StringUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.util.LogE;
import com.orangelife.mobile.app.application.CurryApplication;
import com.orangelife.mobile.constants.Constant;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

/**
 * 友盟分享
 */
public class UMShare {

    private static final java.lang.String TAG = UMShare.class.getSimpleName();
    private static UMShare umengShare;
    private Context context;
    private UMSocialService mController;
    private Activity activity;
    /** 分享url */
    private String targetUrl = "";/* 必须为http连接 */
    /** 分享图片 */
    private UMImage umengImage;
    /** 分享title */
    private String uMtitle = "橙子生活";
    /** 分享content */
    private String uMcontent = "橙子生活，带您玩转社区！";

    Map<String, SHARE_MEDIA> mPlatformsMap = new HashMap<>();

    private UMShare() {
    }

    public static UMShare getInstance() {
        if (umengShare == null) {
            umengShare = new UMShare();
        }
        return umengShare;
    }

    public void init(Context context) {
        this.context = context;
    }

    public void setUMSS(UMSocialService mController, Activity activity) {
        this.mController = mController;
        this.activity = activity;
    }

    public void setShareInfo(String uMtitle, String uMcontent, String url) {
        if (!StringUtil.isBlank(uMtitle)) {
            this.uMtitle = uMtitle;
        } else {
            this.uMtitle = "橙子生活";
        }
        if (!StringUtil.isBlank(uMcontent)) {
            this.uMcontent = uMcontent;
        } else {
            this.uMcontent = "橙子生活，带您玩转社区！";
        }
        if (!URLHelper.isContainHttp(url)) {/*不带域名的URL要加上域名*/
            url = CurryApplication.getInstance().getServerURL() + url;
        }
        this.targetUrl = url;
        LogE.E(TAG, "标题=" + uMtitle + "," + "内容=" + uMcontent + ",url=" + this.targetUrl);
    }

    /**
     * 配置分享平台参数</br>
     */
    public void configPlatforms() {
        // setShare();
        umengImage = new UMImage(context, BitmapFactory.decodeResource(
                activity.getResources(), R.drawable.ic_launcher));
        /* 添加新浪SSO授权*/
        //mController.getConfig().setSsoHandler(new SinaSsoHandler());
        /* 添加腾讯微博SSO授权*/
        //mController.getConfig().setSsoHandler(new TencentWBSsoHandler());
        /* 添加微信、微信朋友圈平台*/
        addWXPlatform();
        addWxCirclePlatform();
        /*添加腾讯SSO授权*/
        addQQPlatfom();
    }

    /**
     * @return
     * @功能描述 : 添加微信平台分享
     */
    private void addWXPlatform() {
        /*
         * 注意：在微信授权的时候，必须传递appSecret wx967daebe835fbeac是你在微信开发平台注册应用的AppID,
		 * 这里需要替换成你注册的AppID String appId = "wx967daebe835fbeac"; String
		 * appSecret = "5bb696d9ccd75a38c8a0bfe0675559b3"; 添加微信平台
		 */
        String wx_appid = context.getResources().getString(R.string.wx_appid);
        String wx_secret = context.getResources().getString(R.string.wx_secret);
        LogE.E(TAG, "微信APP_ID = " + wx_appid);
        LogE.E(TAG, "微信secret = " + wx_secret);
        UMWXHandler wxHandler = new UMWXHandler(context, wx_appid,
                wx_secret);
        wxHandler.addToSocialSDK();
        wxHandler.showCompressToast(false);
    }

    private void addWxCirclePlatform() {
        String wx_appid = context.getResources().getString(R.string.wx_appid);
        String wx_secret = context.getResources().getString(R.string.wx_secret);
        UMWXHandler wxCircleHandler = new UMWXHandler(context,
                wx_appid, wx_secret);
          /* 支持微信朋友圈 */
        wxCircleHandler.setToCircle(true);
        wxCircleHandler.addToSocialSDK();
    }

    /**
     * @return
     * @功能描述 : 添加QQ平台支持 QQ分享的内容， 包含四种类型， 即单纯的文字、图片、音乐、视频. 参数说明 : title, summary,
     * image url中必须至少设置一个, targetUrl必须设置,网页地址必须以"http://"开头 . title :
     * 要分享标题 summary : 要分享的文字概述 image url : 图片地址 [以上三个参数至少填写一个] targetUrl
     * : 用户点击该分享时跳转到的目标地址 [必填] ( 若不填写则默认设置为友盟主页 )
     */
    private void addQZonePlatform() {
        /* 添加QZone平台 */
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(activity, Constant.QQ_APPID,
                Constant.QQ_SECRET);
        qZoneSsoHandler.addToSocialSDK();
    }

    private void addQQPlatfom() {
         /* 添加QQ支持, 并且设置QQ分享内容的target url */
        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(activity, Constant.QQ_APPID,
                Constant.QQ_SECRET);
        qqSsoHandler.addToSocialSDK();
    }

    public void setShare(SHARE_MEDIA platform) {
        if (SHARE_MEDIA.WEIXIN.equals(platform)) {
            setWxShare();
        } else if (SHARE_MEDIA.WEIXIN_CIRCLE.equals(platform)) {
            setWxCircleShare();
        } else if (SHARE_MEDIA.QQ.equals(platform)) {
            setQQShare();
        }
//        else if (SHARE_MEDIA.QZONE.equals(platform)) {
//            setQzoneShare();
//        } else if (SHARE_MEDIA.SINA.equals(platform)) {
//            setSinaShare();
//        }
    }

    /**
     * 设置新浪微博分享
     */
    private void setSinaShare() {
        SinaShareContent sinaContent = new SinaShareContent();
        sinaContent.setShareContent(uMcontent);
        sinaContent.setTargetUrl(targetUrl);
        sinaContent.setTitle(uMtitle);
        sinaContent.setShareImage(umengImage);
        mController.setShareMedia(sinaContent);
    }

    /**
     * 设置QQ
     */
    private void setQQShare() {
        QQShareContent qqShareContent = new QQShareContent();
        qqShareContent.setShareContent(uMcontent);
        qqShareContent.setTargetUrl(targetUrl);
        qqShareContent.setTitle(uMtitle);
        qqShareContent.setShareImage(umengImage);
        mController.setShareMedia(qqShareContent);
    }

    /**
     * 设置QQ空间分享
     */
    private void setQzoneShare() {
        // 设置QQ空间分享内容
        QZoneShareContent qzone = new QZoneShareContent();
        qzone.setShareContent(uMcontent);
        qzone.setTargetUrl(targetUrl);
        qzone.setTitle(uMtitle);
        qzone.setShareImage(umengImage);
        mController.setShareMedia(qzone);
    }

    /**
     * 设置微信分享
     */
    private void setWxShare() {
        String wx_appid = context.getResources().getString(R.string.wx_appid);
        String wx_secret = context.getResources().getString(R.string.wx_secret);
        LogE.E(TAG, "微信APP_ID = " + wx_appid);
        LogE.E(TAG, "微信secret = " + wx_secret);

        WeiXinShareContent weixinContent = new WeiXinShareContent();
        weixinContent.setShareContent(uMcontent);
        weixinContent.setTargetUrl(targetUrl);
        weixinContent.setTitle(uMtitle);
        weixinContent.setShareImage(umengImage);
        mController.setShareMedia(weixinContent);
    }

    private void setWxCircleShare() {
        CircleShareContent circleMedia = new CircleShareContent();
        circleMedia.setShareImage(umengImage);
        circleMedia.setTitle(uMtitle);
        circleMedia.setShareContent(uMcontent);
        circleMedia.setTargetUrl(targetUrl);
        mController.setShareMedia(circleMedia);
    }
}
